package com.jilefojing.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jilefojing.beans.data.enums.ResponseEnum;
import com.jilefojing.beans.data.response.base.ResponseData;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.RFavoriteSource;
import com.jilefojing.beans.pojos.enhanced.EnhancedRFavoriteSource;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.service.IFavoriteService;
import com.jilefojing.service.IRFavoriteSourceService;
import com.jilefojing.web.base.BaseWeb;
import com.jilefojing.web.convent.CommonConvent;

/**
 * 收藏
 * 
 * @author jiangguoshun
 *
 */
@Controller
@RequestMapping("favorite")
public class FavoriteWeb extends BaseWeb {
	@Autowired
	private IFavoriteService favoriteService;
	@Autowired
	private IRFavoriteSourceService rFavoriteSourceService;

	@RequestMapping("add")
	public @ResponseBody ResponseData add(RFavoriteSource rFavoriteSource) {
		ResponseData responseData = new ResponseData();

		try {
			UserContext userContext = super.getUserContext();
			RFavoriteSource dbrfFavoriteSource = rFavoriteSourceService.add(rFavoriteSource, userContext);
			responseData.setData(CommonConvent.convent2RFavoriteSourceData(new EnhancedRFavoriteSource(dbrfFavoriteSource)));
		} catch (UserContextException ex) {
			if (ex.getResponse() != null) {
				responseData.setResponseEnum(ex.getResponse());
			} else {
				responseData.setResponseEnum(ResponseEnum.UNKNOWN);
				responseData.setResponseMessage(ex.getMessage());
			}
		}

		return responseData;
	}

	@RequestMapping("cancle")
	public @ResponseBody ResponseData cancel(RFavoriteSource rFavoriteSource) {
		ResponseData responseData = new ResponseData();

		try {
			UserContext userContext = super.getUserContext();
			rFavoriteSourceService.cancleById(rFavoriteSource.getId(), userContext);
		} catch (UserContextException ex) {
			if (ex.getResponse() != null) {
				responseData.setResponseEnum(ex.getResponse());
			} else {
				responseData.setResponseEnum(ResponseEnum.UNKNOWN);
				responseData.setResponseMessage(ex.getMessage());
			}
		}

		return responseData;
	}

}
